package com.qianchang.microfinance.api;

import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.DMCache;
import com.dm.library.utils.ToastUtil;
import com.qianchang.microfinance.bean.BankCardBean;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseActivity;

/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述: 查询绑定的银行卡详情
 * 修 改 人: jinbailiang
 * 修改时间: 2017/9/11 14:19
 */
public class BankCardDetailApi {

    /**
     * 查询绑定的银行卡详情，
     *
     * @param activity
     * @param callBack
     */
    public static void getBankCardDetail(BaseActivity activity, BankCardDetailCallBack callBack) {
        DMCache dmCache = DMCache.get(activity.getApplicationContext());
        BankCardBean bankCardBean = (BankCardBean) dmCache.getAsObject(DMConstant.DMCACHE_STRING.DM_BANK_CARD_DETAIL);
        if (bankCardBean != null) {
            callBack.refresh(bankCardBean);
        } else {
            queryBankCardDetail(activity, callBack);
        }
    }

    private static void queryBankCardDetail(final BaseActivity activity, final BankCardDetailCallBack callBack) {
        activity.show();
        HttpParams params = new HttpParams();
        DMOKHttpUtils.getInstance().post(activity, DMConstant.ApiURL.FIND_CARD_DETAIL, params, new RequestCallBack<BankCardBean>() {

            @Override
            public void onSuccess(JsonResult<BankCardBean> response) {
                activity.dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    callBack.refresh(response.getObject());
                    DMCache dmCache = DMCache.get(activity.getApplicationContext());
                    dmCache.put(DMConstant.DMCACHE_STRING.DM_BANK_CARD_DETAIL, response.getObject());
                } else {
                    ToastUtil.getInstant().show(activity, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                activity.dismiss();
                ToastUtil.getInstant().show(activity, msg);
            }

            @Override
            public void onConnectFailure(String msg) {

            }
        });

    }

    public interface BankCardDetailCallBack {

        void refresh(BankCardBean bankCardBean);
    }

}
